package com.film.service.impl.account;

import com.film.service.account.LoginService;
import com.film.utils.MySqlConnection;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Override
    public Map<String, String> getToken(String username, String password) {
        Map<String,String> map = new HashMap<>();
        String sql = "select * from User where username = " + "'" + username + "'";
        Statement stmt = null;
        try {
            stmt = MySqlConnection.connect.createStatement();
            ResultSet resultSet = stmt.executeQuery(sql);
            if (!resultSet.isBeforeFirst()) {
                map.put("error_message", "用户名不存在");
                return map;
            } else {
                resultSet.next();
                String savedPassword = resultSet.getString("password");
                if (!savedPassword.equals(password)) {
                    map.put("error_message", "密码错误");
                    return map;
                } else {
                    String token = resultSet.getString("token");
                    String id = resultSet.getString("id");
                    String photo = resultSet.getString("photo_url");
                    map.put("id", id);
                    map.put("username", username);
                    map.put("token", token);
                    map.put("error_message", "success");
                    map.put("photo", photo);
                    System.out.println("登陆成功!");
                    return map;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


        map.put("error_message", "未知错误");
        return map;
    }
}
